package com.tool;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@Component
public class Connecttool {
    private ThreadLocal<Connection>tl=new ThreadLocal<>();
    @Autowired
    private DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    public Connection gettreadconnect(){
        try {
                Connection connection=tl.get();
            if (connection==null)
            {
                    connection=dataSource.getConnection();
                    tl.set(connection);
            }
            return connection;
        } catch (SQLException throwables) {
            throw new RuntimeException("afs");
        }
    }
    public void removeconnect(){
        tl.remove();
    }
}
